package leagal_aid.bean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import leagal_aid.util.DBUtil;

public class HomeBean {
	private int id;
	private String picture;
	private Date time;
	private int homepage;
	

	PreparedStatement statement = null;
	Connection connection = null;
	ResultSet rs = null;
		
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getPicture() {
		return picture;
	}
	public void setPicture(String picture) {
		this.picture = picture;
	}
	public Date getTime() {
		return time;
	}
	public void setTime(Date time) {
		this.time = time;
	}
	public int getHomepage() {
		return homepage;
	}
	public void setHomepage(int homepage) {
		this.homepage = homepage;
	}
	public List<HomeBean> findAll() {
		List<HomeBean> list = new ArrayList<HomeBean>();
		DBUtil dbUtil = new DBUtil();
		connection = dbUtil.geConnection();
		//String sql = "select password from homeBean where username = " + username + "'";
		String sql = "select * from picture order by time desc";
		
		try {
			statement = connection.prepareStatement(sql);
			rs = statement.executeQuery();
			while(rs.next()) {
				HomeBean homeBean = new HomeBean();
				homeBean.setId(rs.getInt("id"));
				homeBean.setPicture(rs.getString("picture"));
				homeBean.setTime(rs.getDate("time"));
				homeBean.setHomepage(rs.getInt("homepage"));
				list.add(homeBean);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			} /*
				 * if (statement != null) { try { statement.close(); } catch (SQLException e) {
				 * e.printStackTrace(); } }
				 */
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return list;
		
	}

	public boolean doDel(int id) {
		boolean del = false;
		DBUtil dbUtil = new DBUtil();
		connection = dbUtil.geConnection();
		//String sql = "select password from admin where username = " + username + "'";
		String sql = "delete from picture where id =?";
		try {
			statement = connection.prepareStatement(sql);
			statement.setInt(1, id);
			int i = statement.executeUpdate();
			if (i>0) {
				del = true;
			}
				
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return del;
		
	}
	public boolean doAdd(String picture,String time,int homepage) {
		boolean add = false;
		DBUtil dbUtil = new DBUtil();
		connection = dbUtil.geConnection();
		//String sql = "select password from admin where username = " + username + "'";
		String sql = "insert into picture(picture,time,homepage) values(?,?,?)";
		
	  try {		
		  PreparedStatement statement = connection.prepareStatement(sql);
			statement.setString(1, picture);
			statement.setString(2, time);
			statement.setInt(3, homepage);
		
			int i = statement.executeUpdate();
			if (i>0) {
				add = true;
			}
			System.out.println(add);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return add;
		
	}
		
	
}
